package org.eclipseuseradmin.internal.ui.wizards;

import javax.security.auth.callback.ConfirmationCallback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipseuseradmin.ui.BaseRoleElementAdapter;
import org.eclipseuseradmin.ui.IRoleElement;
import org.eclipseuseradmin.ui.IUser;
import org.eclipseuseradmin.ui.model.RoleElementModel;
import org.eclipseuseradmin.ui.model.UserModel;
import org.eclipseuseradmin.ui.wizards.BaseNewRoleElementWizard;
import org.eclipseuseradmin.ui.wizards.INewUserWizard;
import org.eclipseuseradmin.ui.wizards.NewUserWizardPage;
import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;

public class NewUserWizard extends BaseNewRoleElementWizard implements
		INewWizard, INewUserWizard {

	private ConfirmationCallback confirmationCallback;

	private NameCallback nameCallback;

	private PasswordCallback passwordCallback;

	public void init(IWorkbench workbench, IStructuredSelection selection,
			ConfirmationCallback confirmationCallback,
			NameCallback nameCallback, PasswordCallback passwordCallback) {

		init(workbench, selection);
		this.confirmationCallback = confirmationCallback;
		this.nameCallback = nameCallback;
		this.passwordCallback = passwordCallback;
	}

	@Override
	public void addPages() {

		addPage(new NewUserWizardPage());
	}

	@Override
	public RoleElementModel getModel() {
		if (model == null) {
			model = new UserModel(getUserAdmin());
		}
		return model;
	}

	@Override
	public boolean performFinish() {

		UserModel model = (UserModel) getModel();

		User user = (User) getUserAdmin()
				.createRole(model.getName(), Role.USER);

		IUser adapter = (IUser) getAdapterManager().getAdapter(user,
				IUser.class);

		adapter.setObjectClass("user");
		adapter.setOrganizationUnit("users");
		adapter.setPassword(model.getPassword());

		IStructuredSelection selection = getSelection();
		if (selection != null) {
			Object parent = selection.getFirstElement();
			if (parent != null && parent instanceof Role) {
				IRoleElement roleElement = BaseRoleElementAdapter
						.adapt((Role) parent);
				if (roleElement != null && roleElement.acceptChild(user)) {

					roleElement.addChild(user);
					adapter.addParent((Group) parent);
				}

			}
		}

		selectAndReveal(user);

		if (confirmationCallback != null) {

			confirmationCallback.setSelectedIndex(ConfirmationCallback.OK);
			nameCallback.setName(model.getName());
			passwordCallback.setPassword(model.getPassword().toCharArray());
		}

		return true;
	}

	public boolean performCancel() {

		if (confirmationCallback != null) {
			confirmationCallback.setSelectedIndex(ConfirmationCallback.CANCEL);
		}
		return true;
	}

}
